﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Metadata.Entity;
using T.FAS.Runtime.Metadata.Entity.DataModel;

namespace T.FAS.Runtime.DataModel.Plugin
{
    public class EntityObjectManager
    {

        public const string MetaDataID = DataModelConst.EOMetaType;
        public const string MetaDataName = "实体对象";
        private readonly EntityObjectLogic _entityObjectLogic = new EntityObjectLogic();
        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public IMetaDataContent GetMetaDataContent(string metaDataId)
        {
            if (string.IsNullOrEmpty(metaDataId))
                throw new FasException(DataModelErrorCode.NullValue, "元数据id为空");
            var eo = _entityObjectLogic.GetEntityObjectByID(metaDataId);
            if (eo == null)
                throw new FasException(DataModelErrorCode.ParamError, $"未找到{MetaDataName}:{metaDataId}的元数据");

            eo.ID = string.IsNullOrEmpty(eo.ID) ? eo.MetaDataID : eo.ID;
            eo.MetaDataID = string.IsNullOrEmpty(eo.MetaDataID) ? eo.ID : eo.MetaDataID;
            eo.Code = string.IsNullOrEmpty(eo.Code) ? eo.MetaDataCode : eo.Code;
            eo.MetaDataCode = string.IsNullOrEmpty(eo.MetaDataCode) ? eo.Code : eo.MetaDataCode;
            eo.Name = string.IsNullOrEmpty(eo.Name) ? eo.MetaDataName : eo.Name;
            eo.MetaDataName = string.IsNullOrEmpty(eo.MetaDataName) ? eo.Name : eo.MetaDataName;

            return eo;

        }

        #region 导入导出
        public void Import(List<IMetaDataContent> metaDatas)
        {
            var entityObjects = new List<EntityObject>();
            foreach (var metaData in metaDatas)
            {
                var entity = metaData as EntityObject;
                if (entity == null)
                {
                    throw new FasException(DataModelErrorCode.ParamError, "元数据类型不是EntityObject, 实际类型:" + metaData.GetType().FullName);
                }

                entityObjects.Add(entity);
            }
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    foreach (var entity in entityObjects)
                    {
                        _entityObjectLogic.DeleteEntityObject(entity.MetaDataID);
                        _entityObjectLogic.SaveEntityObject(entity);
                    }

                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.EO.ImportError, "EntityObject元数据导入失败", e);
            }
        }
        #endregion

        public void Delete(List<string> IDs)
        {
            if (IDs == null)
                return;
            IDs = IDs.Where(id => id != null).ToList();
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted }))
                {
                    foreach (var entity in IDs)
                    {
                        _entityObjectLogic.DeleteEntityObject(entity);
                    }

                    scope.Complete();
                }
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.EO.ImportError, $"{MetaDataName}元数据导入时删除失败", e);
            }
        }

    }
}
